import Vue from 'vue';
import Vuex from 'vuex';

import request from '../utils/request.js';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    // token
    token: '',
    userInfo: '',
  },
  mutations: {
    getToken(state, token) {
      state.token = token;
    },
    //  存储用户信息
    setUserInfo(state, user) {
      state.userInfo = user;
    },
  },
  actions: {
    //  获取用户信息
    getUserInfoX(context) {
      //  获取 用户信息
      if (localStorage.getItem('token')) {
        request({
          method: 'post',
          headers: { Authorization: localStorage.getItem('token') },
          url: 'http://47.108.147.79:8088/user/currentUser',
        }).then(res => {
          const user = res.data.data;
          context.commit('setUserInfo', user);
          // console.log(user);
        });
      }
    },
  },
  modules: {},
});
